<template>
    <div class="container">
      <h1 class="title">账号信息编辑</h1>
  
      <form @submit.prevent="submitForm" class="form" >
        
        <div class="form-group">
          <label for="username">用户名</label>
          <input id="username" v-model="user.username" type="text" required />
        </div>
  
        <div class="form-group">
          <label for="nickname">昵称</label>
          <input id="nickname" v-model="user.nickname" type="text" required />
        </div>
        
        <div class="form-group">
          <label for="email">邮箱</label>
          <input id="email" v-model="user.email" type="email" required />
        </div>
        
        <div class="form-group">
          <label for="phone">手机号</label>
          <input id="phone" v-model="user.phone" type="tel" required />
        </div>
  
        <div class="form-actions">
          <button type="submit">保存信息</button>
        </div>

      </form>
      <button class="upload" @click="showModal = true">修改头像</button>
      <FileForm 
        v-model:visible="showModal"
        @upload-success="handleUploadSuccess"
      />
    </div>
  </template>
  
<script setup>
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router';
import { userGet, userSet } from '@/api/BlogUser';
import { emitter } from '@/router/eventBus';
import server from '@/api/server';
import FileForm from '@/components/FileForm.vue';

const router = useRouter();

const showModal = ref(false);

const user = ref({
  username: '',
  nickname: '',
  email: '',
  phone: '',
  avatar: ''
});

const handleUploadSuccess = (url) => {
  user.value.avatar = url;
  alert("上传成功: "+ url);
};

function submitForm() {
  try {
    return userSet(server.token, user.value).then((res)=>{
      if(res.data.code == 0){
        user.value.username = res.data.data.username
        user.value.nickname = res.data.data.nickname
        user.value.email = res.data.data.email
        user.value.phone = res.data.data.phone
        user.value.avatar = res.data.data.avatar
        emitter.emit("avatar_change", res.data)
        alert('用户信息修改成功');
      }else{

        alert('用户信息修改失败: '+res.data.msg);

      }
    })
  } catch (error) {

    alert('用户信息修改失败: '+error);

  }
}

onMounted(async () => {
  if(server.token == ''){
    router.push("/notLogin")
    return
  }
  try {
    await userGet(server.token).then((res)=>{
      if(res.data.code == 0){
        user.value.username = res.data.data.username
        user.value.nickname = res.data.data.nickname
        user.value.email = res.data.data.email
        user.value.phone = res.data.data.phone
        user.value.avatar = res.data.data.avatar
      }else{

        alert('获取用户信息失败');

      }})
  } catch (error) {

    alert('获取用户信息失败: '+error);

  }
})
</script>
  




<style scoped>
.container {
  max-width: 500px;
  margin: 80px auto;
  padding: 40px 20px;
  font-family: 'Helvetica Neue', sans-serif;
  background-color: #fff;
}

.title {
  text-align: center;
  font-size: 28px;
  color: #333;
  margin-bottom: 30px;
}

.form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
}

label {
  font-size: 14px;
  margin-bottom: 6px;
  color: #555;
}

input {
  padding: 10px 12px;
  border: 1px solid #ccc;
  border-radius: 8px;
  font-size: 16px;
  transition: border 0.3s;
}

input:focus {
  border-color: #007bff;
  outline: none;
}

.form-actions {
  text-align: center;
  margin-top: 20px;
}

button {
  background-color: #007bff;
  color: white;
  padding: 10px 24px;
  font-size: 16px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.3s;
}
button.upload{
  display: block; margin: 20px auto;
}

button:hover {
  background-color: #0056b3;
}
</style>
  